<template>
  <view class="container">
    <!-- 顶部按钮组 -->
    <view class="menu-btn" style="z-index: 102">
      <view class="dot-text Flex iconfont icon-fanhui" @click="goBack"></view>
      <view class="dot-text Flex iconfont icon-palette-line" @click="openColor"></view>
      <view class="dot-text Flex iconfont icon-shezhi" @click.stop="openInput"></view>
    </view>

    <canvas type="2d" id="myCanvas" style="z-index: 0" class="canvas"></canvas>

    <!-- 弹窗蒙层和输入框 -->
    <view class="modal-overlay" v-show="showInput" @click="cancelInput"></view>
    <view class="input-box" v-show="showInput">
      <view class="title">✨ 自定义文案 ✨</view>
      <input type="text" maxLength="12" v-model="tempText" placeholder="请输入要显示的文字" />
      <view class="button-group">
        <button class="cancel-btn" @click="cancelInput">取消</button>
        <button class="confirm-btn" @click="confirmInput">确定</button>
      </view>
    </view>

    <uv-pick-color ref="pickerColor" @confirm="onColorChange"></uv-pick-color>
  </view>
</template>

<script>
export default {
  data() {
    return {
      ctx: null,
      width: 0,
      height: 0,
      text: "2025年新年快乐！！！",
      scrollY: 0,
      currentColor: "#39b54a",
      showInput: false,
      tempText: "",
      timer: null,
      fontSize: 180,
      speed: 1
    }
  },

  onReady() {
    this.initCanvas()
  },

  methods: {
    async initCanvas() {
      const sysInfo = uni.getSystemInfoSync()
      this.width = sysInfo.windowWidth
      this.height = sysInfo.windowHeight

      const query = uni.createSelectorQuery().in(this)
      query.select('#myCanvas')
        .fields({ node: true, size: true })
        .exec((res) => {
          const canvas = res[0].node
          const ctx = canvas.getContext('2d')
          
          canvas.width = this.width
          canvas.height = this.height
          
          this.ctx = ctx
          this.ctx.font = `${this.fontSize}px Arial`
          this.ctx.textBaseline = 'middle'
          this.ctx.textAlign = 'center'
          
          // 初始化位置
          this.scrollY = this.height + this.fontSize
          
          this.startScroll()
        })
    },

    startScroll() {
      if (this.timer) {
        clearInterval(this.timer)
      }

      this.timer = setInterval(() => {
        this.ctx.fillStyle = '#000'
        this.ctx.fillRect(0, 0, this.width, this.height)
        
        this.ctx.fillStyle = this.currentColor
        this.ctx.shadowColor = this.currentColor
        this.ctx.shadowBlur = 20
        
        // 绘制文字
        this.ctx.save()
        this.ctx.translate(this.width / 2, this.scrollY)
        this.ctx.rotate(Math.PI / 2)
        this.ctx.fillText(this.text, 0, 0)
        this.ctx.restore()
        
        // 更新位置
        this.scrollY -= this.speed
        
        // 当文字完全离开屏幕时重置位置
        if (this.scrollY < -this.fontSize) {
          this.scrollY = this.height + this.fontSize
        }
      }, 16)
    },

    openColor() {
      this.$refs.pickerColor.open()
    },

    onColorChange(e) {
      this.currentColor = e.hex
      this.initCanvas()
    },

    goBack() {
      uni.navigateBack()
    },

    openInput() {
      this.showInput = true
      this.tempText = this.text
    },

    cancelInput() {
      this.showInput = false
      this.tempText = ""
    },

    confirmInput() {
      if (this.tempText.trim()) {
        this.text = this.tempText
        this.initCanvas()
      }
      this.showInput = false
      this.tempText = ""
    }
  },

  onUnmounted() {
    if (this.timer) {
      clearInterval(this.timer)
    }
  }
}
</script>

<style>
page {
  background-color: #000;
}

.canvas {
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 1;
}

.topBtn {
  position: fixed;
  top: var(--status-bar-height);
  right: 20rpx;
  z-index: 101;
}

.color-picker {
  display: flex;
  align-items: center;
  background: rgba(255, 255, 255, 0.2);
  padding: 10rpx 20rpx;
  border-radius: 30rpx;
}

.color-picker text {
  color: #fff;
  font-size: 24rpx;
  margin-right: 10rpx;
}

.color-preview {
  width: 30rpx;
  height: 30rpx;
  border-radius: 50%;
  border: 2rpx solid rgba(255, 255, 255, 0.5);
}

.menu-btn {
  position: fixed;
  top: 56rpx;
  left: 20rpx;
  min-width: 70px;
  height: 60rpx;
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12rpx;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  z-index: 100;
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 50px;
}
.dot-text {
  font-size: 32rpx;
  color: #fff;
  width: 40px;
  height: 100%;
}
.dot {
  width: 6rpx;
  height: 6rpx;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.6);
}

.menu-btn:active {
  background: rgba(255, 255, 255, 0.2);
}
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(8px);
  z-index: 998;
}

.input-box {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background: rgba(255, 255, 255, 0.15);
  backdrop-filter: blur(15px);
  padding: 40rpx;
  width: 90%;
  box-sizing: border-box;
  border-radius: 30rpx;
  text-align: center;
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
  z-index: 999;
}

.input-box .title {
  color: #fff;
  font-size: 32rpx;
  margin-bottom: 30rpx;
  font-weight: 500;
}

.input-box input {
  height: 88rpx;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 16rpx;
  padding: 0 30rpx;
  color: #fff;
  font-size: 28rpx;
  margin-bottom: 30rpx;
}

.input-box input::placeholder {
  color: rgba(255, 255, 255, 0.5);
}

.button-group {
  display: flex;
  justify-content: space-between;
  gap: 30rpx;
}

.input-box button {
  width: 45%;
  height: 80rpx;
  line-height: 80rpx;
  padding: 0 40rpx;
  border-radius: 36rpx;
  font-size: 28rpx;
  font-weight: normal;
  transition: all 0.3s ease;
}

.cancel-btn {
  background: rgba(255, 255, 255, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.confirm-btn {
  background: linear-gradient(135deg, #007aff, #00c6ff);
  border: none;
}

.input-box button:active {
  transform: scale(0.98);
  opacity: 0.9;
}
</style>